package com.example.moilapp.service;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.moilapp.Moil_Pending_Tasks;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Moil_Login_Service  extends AsyncTask<String, Void, String> {

	private ProgressDialog progdialod;
	private Activity activity;
	private Context context;
	
	private String userName;
	private String password;
	
	// connection establish timeout (timeoutConnection)
	private int timeoutConnection = 5000;
	// socket timeout (timeoutSocket) 
	private int timeoutSocket = 45000;
	
	private static byte[] buff =new byte[1024];
	private String retval="";
	
	private ArrayAdapter<String>	listAdapter;
	
	private int[] app_ids;

	JSONArray jArray;
	private static final String SOAP_ACTION = "http://tempuri.org/Login";

	//http://t2a1.cloudapp.net/TrafficWorkflow/TraficWorkflowWebService.asmx
	
    private static final String METHOD_NAME = "Login";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://t2a1.cloudapp.net/TrafficWorkflow/TraficWorkflowWebService.asmx";
	private static final String TAG = null;
	
	public Moil_Login_Service(Activity activity,String user,String password) {
		super();
		this.activity = activity;
		this.context = this.activity.getApplicationContext();
		this.userName = user;
		this.password = password;
	}
	
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progdialod = ProgressDialog.show(this.activity, "User Login", "Logging In, Please Wait",true,false);
	}
	
	
	@Override
	protected String doInBackground(String... urls) {
		
		 try {

	            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

	            request.addProperty("LoginId", userName);
	            request.addProperty("Password", password);

	            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	            envelope.dotNet=true;
	            envelope.setOutputSoapObject(request);

	            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	            androidHttpTransport.call(SOAP_ACTION, envelope);
	            
	            SoapPrimitive resultSoapPrimitive;
	            resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
	                
	            String result = resultSoapPrimitive.toString();

	            retval = result;
	            
	            Log.d("return", retval);
	            
	            return retval;
	           
	        } catch (Exception e) {
	            
	       }
		 return retval;
		
	}
	
	@Override
	protected void onPostExecute(String result) {
		
		Log.d("Post Exec", "POST EXEC");
		
		/*Toast toast = Toast.makeText(activity, result, Toast.LENGTH_SHORT);
		toast.show();*/
		
		Log.d("Post tt", result);
		progdialod.dismiss();
		
		String User_ID = "";
		String Status = "";
		
		Log.d("Post Exec", result);
		
		try {
			JSONObject jArrayobj = new JSONObject(result);
			
			String user_id = jArrayobj.getString("UserID");
			String user_name = jArrayobj.getString("UserName");
			String status = jArrayobj.getString("Status");
			User_ID = user_id;
			Status = status;
			
		} catch (JSONException e) {
			e.printStackTrace();
		}

		
		if(Status.equals("Success")){
			final Intent intent = new Intent(activity, Moil_Pending_Tasks.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			Bundle bundle = new Bundle();
			
			Log.e("UID", User_ID);
					
			bundle.putString("uid", User_ID);  
					
			intent.putExtras(bundle);
			context.startActivity(intent);
		}else{
			Toast toast = Toast.makeText(activity, "UserName Or Password Incorrect. Login Failed !!", Toast.LENGTH_SHORT);
			toast.show();
		}
		
	}

}
